package tuik

import (
	"fmt"
	"github.com/nsf/termbox-go"
)

func InputMode(inputmode string) {
	switch inputmode {
	case "esc":
		termbox.SetInputMode(termbox.InputEsc)
	case "alt":
		termbox.SetInputMode(termbox.InputAlt)
	case "mouse":
		termbox.SetInputMode(termbox.InputMouse)
	}
}
func Input() string {
	var rekey string
	switch ev := termbox.PollEvent(); ev.Type {
	case termbox.EventKey:
		switch ev.Key {
		case termbox.KeyF1:
			rekey = "F1"
		case termbox.KeyF2:
			rekey = "F2"
		case termbox.KeyF3:
			rekey = "F3"
		case termbox.KeyF4:
			rekey = "F4"
		case termbox.KeyF5:
			rekey = "F5"
		case termbox.KeyF6:
			rekey = "F6"
		case termbox.KeyF7:
			rekey = "F7"
		case termbox.KeyF8:
			rekey = "F8"
		case termbox.KeyF9:
			rekey = "F9"
		case termbox.KeyF10:
			rekey = "F10"
		case termbox.KeyF11:
			rekey = "F11"
		case termbox.KeyF12:
			rekey = "F12"
		case termbox.KeyInsert:
			rekey = "Ins"
		case termbox.KeyDelete:
			rekey = "Del"
		case termbox.KeyHome:
			rekey = "Home"
		case termbox.KeyEnd:
			rekey = "End"
		case termbox.KeyPgup:
			rekey = "Pgup"
		case termbox.KeyPgdn:
			rekey = "Pgdn"
		case termbox.KeyArrowUp:
			rekey = "Up"
		case termbox.KeyArrowDown:
			rekey = "Down"
		case termbox.KeyArrowLeft:
			rekey = "Left"
		case termbox.KeyArrowRight:
			rekey = "Right"

		case termbox.MouseLeft:
			rekey = "MouseLeft"
		case termbox.MouseMiddle:
			rekey = "MouseMiddle"
		case termbox.MouseRight:
			rekey = "MouseRight"

			//		case termbox.KeyCtrlTilde:
			//			rekey = "CtrlTilde"

			//		case termbox.KeyCtrl2:
			//			rekey = "Ctrl2"
			//		case termbox.KeyCtrl3:
			//			rekey = "Ctrl3"
			//		case termbox.KeyCtrl4:
			//			rekey = "Ctrl4"
			//		case termbox.KeyCtrl5:
			//			rekey = "Ctrl5"
		case termbox.KeyCtrl6:
			rekey = "Ctrl6"
		case termbox.KeyCtrl7:
			rekey = "Ctrl7"
			//		case termbox.KeyCtrl8:
			//			rekey = "Ctrl8"
			//		case termbox.KeyCtrlSpace:
			//			rekey = "CtrlSpace"
		case termbox.KeyCtrlA:
			rekey = "CtrlA"
		case termbox.KeyCtrlB:
			rekey = "CtrlB"
		case termbox.KeyCtrlC:
			rekey = "CtrlC"
		case termbox.KeyCtrlD:
			rekey = "CtrlD"
		case termbox.KeyCtrlE:
			rekey = "CtrlE"
		case termbox.KeyCtrlF:
			rekey = "CtrlF"
		case termbox.KeyCtrlG:
			rekey = "CtrlG"
			//		case termbox.KeyCtrlH:
			//			rekey = "CtrlH"
			//		case termbox.KeyCtrlI:
			//			rekey = "CtrlI"
		case termbox.KeyCtrlJ:
			rekey = "CtrlJ"
		case termbox.KeyCtrlK:
			rekey = "CtrlK"
		case termbox.KeyCtrlL:
			rekey = "CtrlL"
			//		case termbox.KeyCtrlM:
			//			rekey = "CtrlM"
		case termbox.KeyCtrlN:
			rekey = "CtrlN"
		case termbox.KeyCtrlO:
			rekey = "CtrlO"
		case termbox.KeyCtrlP:
			rekey = "CtrlP"
		case termbox.KeyCtrlQ:
			rekey = "CtrlQ"
		case termbox.KeyCtrlR:
			rekey = "CtrlR"
		case termbox.KeyCtrlS:
			rekey = "CtrlS"
		case termbox.KeyCtrlT:
			rekey = "CtrlT"
		case termbox.KeyCtrlU:
			rekey = "CtrlU"
		case termbox.KeyCtrlV:
			rekey = "CtrlV"
		case termbox.KeyCtrlW:
			rekey = "CtrlW"
		case termbox.KeyCtrlX:
			rekey = "CtrlX"
		case termbox.KeyCtrlY:
			rekey = "CtrlY"
		case termbox.KeyCtrlZ:
			rekey = "CtrlZ"
		case termbox.KeyBackspace:
			rekey = "Backspace"
		case termbox.KeyTab:
			rekey = "Tab"
		case termbox.KeyEnter:
			rekey = "Enter"
		case termbox.KeyEsc:
			rekey = "Esc"
			//		case termbox.KeyCtrlLsqBracket:
			//			rekey = "CtrlLsqBracket"
		case termbox.KeyCtrlBackslash:
			rekey = "CtrlBackslash"
		case termbox.KeyCtrlRsqBracket:
			rekey = "CtrlRsqBracket"
			//		case termbox.KeyCtrlSlash:
			//			rekey = "CtrlSlash"
			//		case termbox.KeyCtrlUnderscore:
			//			rekey = "CtrlUnderscore"
		case termbox.KeySpace:
			rekey = "Space"
		case termbox.KeyBackspace2:
			rekey = "Backspace2"
		default:
			if ev.Ch != 0 {
				rekey = keyname[fmt.Sprint(ev.Ch)]
			}
		}
	}
	return rekey
}

var keyname = map[string]string{
	"97":  "a",
	"98":  "b",
	"99":  "c",
	"100": "d",
	"101": "e",
	"102": "f",
	"103": "g",
	"104": "h",
	"105": "i",
	"106": "j",
	"107": "k",
	"108": "l",
	"109": "m",
	"110": "n",
	"111": "o",
	"112": "p",
	"113": "q",
	"114": "r",
	"115": "s",
	"116": "t",
	"117": "u",
	"118": "v",
	"119": "w",
	"120": "x",
	"121": "y",
	"122": "z",
	"96":  "`",
	"49":  "1",
	"50":  "2",
	"51":  "3",
	"52":  "4",
	"53":  "5",
	"54":  "6",
	"55":  "7",
	"56":  "8",
	"57":  "9",
	"48":  "0",
	"45":  "-",
	"61":  "=",
	"65":  "A",
	"66":  "B",
	"67":  "C",
	"68":  "D",
	"69":  "E",
	"70":  "F",
	"71":  "G",
	"72":  "H",
	"73":  "I",
	"74":  "J",
	"75":  "K",
	"76":  "L",
	"77":  "M",
	"78":  "N",
	"79":  "O",
	"80":  "P",
	"81":  "Q",
	"82":  "R",
	"83":  "S",
	"84":  "T",
	"85":  "U",
	"86":  "V",
	"87":  "W",
	"88":  "X",
	"89":  "Y",
	"90":  "Z",
	"126": "~",
	"33":  "!",
	"64":  "@",
	"35":  "#",
	"36":  "$",
	"37":  "%",
	"94":  "^",
	"38":  "&",
	"42":  "*",
	"40":  "(",
	"41":  ")",
	"95":  "_",
	"43":  "+",
	"91":  "[",
	"93":  "]",
	"123": "{",
	"125": "}",
	"59":  ";",
	"39":  "'",
	"58":  ":",
	"34":  "\"",
	"44":  ",",
	"46":  ".",
	"60":  "<",
	"62":  ">",
	"47":  "/",
	"63":  "?",
	"92":  "\\",
	"124": "|"}
